package com.hmall.service;

import com.google.gson.Gson;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.util.PdfUtil;
import com.hmall.vo.PrintProductVo;
import com.hmall.vo.ProductPlanOrderVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

//import com.hmall.util.PDFHelper;

/**
 * PDF服务层
 */
@Service
public class PdfService {
    @Autowired
    UpDownServiceImpl upDownService;
    public ServerResponse<String> showProductPlanPdf(String result){

        /*ProductPlanOrderVo printProductVo1=new ProductPlanOrderVo("aa","aaa","aa","AA","ddd","aa","aa","aa","aa","aa",1000);
        ProductPlanOrderVo printProductVo2=new ProductPlanOrderVo("aa","aaa","aa","AA","ddd","aa","aa","aa","aa","aa",1000);
        List<ProductPlanOrderVo> products=new ArrayList<ProductPlanOrderVo>();
        products.add(printProductVo1);
        products.add(printProductVo2);
        PrintProductVo  printProductVo =new PrintProductVo();
        printProductVo.setProductPlanOrderVoList(products);
        printProductVo.setDate("2018-05-05");
        printProductVo.setName("测试");
        printProductVo.setRemark("备注");
        printProductVo.setTotalWeigth(15555);*/
        Gson gson = new Gson();
        PrintProductVo printProductVo = gson.fromJson(result, PrintProductVo.class);
//        System.out.println(map2.get("productPlanOrderVoList"));
        JSONArray jsonArray = JSONArray.fromObject(printProductVo.getProductPlanOrderVoList());
        for (int i=0;i<jsonArray.size();i++){
            ProductPlanOrderVo product = (ProductPlanOrderVo)JSONObject.toBean((JSONObject) jsonArray.get(i),ProductPlanOrderVo.class);
            System.out.println(product.getColor());
        }
        String planPath=PdfUtil.orderShowPlanPdfUpload(printProductVo);
        String pathPDF="pdf";
        File PDFFile=new File(pathPDF);
        //如果文件夹不存在则创建
        if  (!PDFFile.exists()  && !PDFFile.isDirectory())
        {
            PDFFile.mkdir();
        }
        //如果文件夹不存在则创建
        FileInputStream is= null;
        try {
            is = new FileInputStream(planPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //上传pdf
        String pdfUrl=upDownService.updatePDFHead(printProductVo.getName()+".pdf",is);
        //删除本地图片和pdf
        /*File PDFPath=new File(pathPDF+File.separator+fileName);
        if (PDFPath.exists()){
            PDFPath.delete();
        }*/
        System.out.println("图片上传成功！！");
        return ServerResponse.createBySuccess(pdfUrl);

    }

    public static void main(String[] args) {

    }

}
